package com.kkb.service;


import com.kkb.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserTest {
    public static void main(String[] args) {
        try {
            findAllHandler();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() throws IOException {
        // 加载MyBatis核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 创建工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        // 工厂生产sqlSession对象，用于执行sql语句。（queryRunner）
        SqlSession sqlSession = factory.openSession();
        return sqlSession;
    }

    public static void findAllHandler() throws IOException {
        SqlSession sqlSession = getSqlSession();
        List<User> userLists = sqlSession.selectList("UserMap.findAll");
        for (User user : userLists) {
            System.out.println("user = " + user);
        }
        // 关闭资源
        sqlSession.close();
    }
}
